package com.hxj.common.thread.event;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Component;

/**
 * @author Tz
 * @date 2024/02/23 17:15
 * @Description 动态线程池的销毁时间事件
 */
@Component
public class DynamicThreadPoolCleanupListener implements ApplicationListener<ContextClosedEvent> {

    private final DynamicThreadPoolManager dynamicThreadPoolManager;

    public DynamicThreadPoolCleanupListener(DynamicThreadPoolManager dynamicThreadPoolManager) {
        this.dynamicThreadPoolManager = dynamicThreadPoolManager;
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        //进行销毁前对线程池进行手动释放
        dynamicThreadPoolManager.shutdownAllThreadPools();
    }
}
